package com.core.ui.activity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.Window;

import com.core.ActivityStack;
import com.core.exception.CrashHandler;
import com.core.listener.I_Activity;
import com.core.type.State;
import com.core.util.L;
import com.jxzyk.config.Constant;
import com.lidroid.xutils.ViewUtils;

/**
 * 
 * 
 * @Description: TODO(带有Fragment.v4切换的主界面)
 * 
 * @author gufei 562401002@qq.com
 * 
 * @date 2015年3月17日 上午10:46:37
 */
public abstract class BaseMainActivity extends FragmentActivity implements
		I_Activity {

	/************** Activity的状态 **************/
	public State activityState = State.DESTROY;
	public boolean isBack = false;
	private Bundle bundle;

	public Bundle getBundle() {
		return bundle;
	}

	public void setBundle(Bundle bundle) {
		this.bundle = bundle;
	}

	@Override
	public void showActivity(Class<?> cls) {
		startActivity(new Intent(this, cls));
	}

	@Override
	public void showActivity(String url) {
		Uri uri = Uri.parse(url);
		Intent intent = new Intent(Intent.ACTION_VIEW, uri);
		startActivity(intent);
	}

	@Override
	public void showActivity(Class<?> cls, Bundle bundle) {
		Intent intent = new Intent(this, cls);
		intent.putExtras(bundle);
		startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); // 设置竖屏
		requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置无标题
		L.state(getClass(), "onCreate");
		super.onCreate(savedInstanceState);
		ActivityStack.create().addActivity(this);
		ViewUtils.inject(this);
		CrashHandler.instance(this);
		initData(savedInstanceState);
		initUi();
	}

	@Override
	protected void onStart() {
		L.state(getClass(), "onStart");
		super.onStart();
	}

	@Override
	protected void onResume() {
		L.state(getClass(), "onResume");
		super.onResume();
		activityState = State.RESUME;
	}

	@Override
	protected void onPause() {
		L.state(getClass(), "onPause");
		super.onPause();
		activityState = State.PAUSE;
	}

	@Override
	protected void onStop() {
		L.state(getClass(), "onStop");
		super.onStop();
		activityState = State.STOP;
	}

	@Override
	protected void onRestart() {
		L.state(getClass(), "onRestart");
		super.onRestart();
	}

	@Override
	protected void onDestroy() {
		L.state(getClass(), "onDestroy");
		activityState = State.DESTROY;
		super.onDestroy();
		ActivityStack.create().finishActivity(this);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			if (isBack) {
				Intent intent = new Intent(Intent.ACTION_MAIN);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.addCategory(Intent.CATEGORY_HOME);
				startActivity(intent);
			}
			break;
		default:
			break;
		}
		return super.onKeyDown(keyCode, event);
	}
}
